package com.college.heyuhua.controllers;

import com.college.heyuhua.entity.UserInfo;
import com.college.heyuhua.service.IUserRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.logging.Logger;

/**
 * Created by 赵东坡 on 2018/12/30.
 * Time:21:02
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    private static Logger logger = Logger.getLogger("RegisterController.class");

    @Autowired
    private IUserRegister iUserRegister;

    @RequestMapping("/userregister")
    public String register() {
        return "register";
    }

    @RequestMapping("/getUserInfo")
    public String register(UserInfo userInfo, ModelMap map) {

        if(StringUtils.isEmpty(userInfo.getUsername())||
                StringUtils.isEmpty(userInfo.getRealname())||
                StringUtils.isEmpty(userInfo.getPassword())||
                StringUtils.isEmpty(userInfo.getAge())||
                StringUtils.isEmpty(userInfo.getMajor())||
                StringUtils.isEmpty(userInfo.getSex())||
                StringUtils.isEmpty(userInfo.getStuclass())||
                StringUtils.isEmpty(userInfo.getTelephone())||
                StringUtils.isEmpty(userInfo.getStunumber())||
                StringUtils.isEmpty(userInfo.getWeixin())) {
            map.put("message","注册失败，信息不全");
            return "register";
        }else{
            //TODO 放到redis中
            try{
                iUserRegister.registry(userInfo);
            }
            catch (Exception e){
                logger.info("【register】注册失败");
                map.put("message","注册失败,重要信息已存在");
                return "register";
            }
        }
        return "redirect";
    }
}
